package com.dianrui.fmi.fragment;

import android.os.Handler;

import android.widget.ListView;

import com.dianrui.fmi.R;
import com.dianrui.fmi.adapter.FaqAdapter;
import com.dianrui.fmi.bean.WarmPrompt;
import com.dianrui.fmi.event.RfreshFaq;
import com.dianrui.fmi.util.OkHttpUtils;
import com.dianrui.fmi.util.StringUtils;
import com.dianrui.fmi.util.Url;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


public class FaqFragment extends BaseFragment {

    private List<WarmPrompt> warmPromptList = new ArrayList<>();
    private FaqAdapter faqAdapter;

    @BindView(R.id.faq_listview)
    ListView faqList;


    @Override
    public int getContentViewId() {
        return R.layout.frag_faq;
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        faqAdapter=new FaqAdapter(getActivity(),warmPromptList);
        faqList.setAdapter(faqAdapter);
        getFaqData();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(RfreshFaq rfreshFaq) {
        getFaqData();
    }


    private void getFaqData() {
        OkHttpUtils.getInstance().postRequests(Url.INDEX, "",  new OkHttpUtils.XCallBack() {
            @Override
            public void onResponse(String result) {
                if (!StringUtils.isEmpty(result)) {
                    try {
                        final JSONObject jsonObject = new JSONObject(result);
                        if (null != jsonObject && isAdded()) {
                            if ("1000".equals(jsonObject.optString("status"))) {
                                if (warmPromptList != null) {
                                    warmPromptList.clear();
                                }
                                JSONArray warm_prompt = jsonObject.optJSONObject("data").optJSONArray("warm_prompt");
                                if (warm_prompt.length() > 0) {
                                    for (int i = 0; i < warm_prompt.length(); i++) {
                                        WarmPrompt warmPrompt = new WarmPrompt();
                                        warmPrompt.explain_id = warm_prompt.optJSONObject(i).optString("explain_id");
                                        warmPrompt.title = warm_prompt.optJSONObject(i).optString("title");
                                        warmPrompt.jump_url = warm_prompt.optJSONObject(i).optString("jump_url");
                                        warmPromptList.add(warmPrompt);
                                    }
                                    if (faqAdapter != null) {
                                        faqAdapter.refresh(warmPromptList);
                                    }
                                }
                            }
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    @Override
    public void getData() {}

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }
}
